/** 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package it.free.i.virtual.machine.hal.devicealias.laboratory.actuators;

import it.free.i.virtual.machine.hal.devicealias.DeviceAliasCommandExecutionException;
import it.free.i.virtual.machine.hal.devicealias.command.DeviceAliasPortCommand;
import it.free.i.virtual.machine.hal.devicealias.command.DeviceAliasPortCommandImpl;
import it.free.i.virtual.machine.hal.devicealias.command.DeviceAliasPortCommandVerb;
import it.free.i.virtual.machine.hal.devicealias.controller.DeviceAliasPortController;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Keeps a prototype for every actuator command available. You can fire via this class an actuator command passing a
 * value from 1 to 4
 * @author stefano
 */
public class EffectUnawareActuatorFireAbleCommandPrototypesCatalog extends ArrayList<DeviceAliasPortCommand> {

	//On class loading pre load prototypes
	static{
		EffectUnawareActuatorFireAbleCommandPrototypesCatalog.doPrepare();
	}
	
	private static final long serialVersionUID = 5174931563333361254L;
	
	private static EffectUnawareActuatorFireAbleCommandPrototypesCatalog effectAgnosticActuatorFireAbleCommandPrototypesCatalog = new EffectUnawareActuatorFireAbleCommandPrototypesCatalog();

	private EffectUnawareActuatorFireAbleCommandPrototypesCatalog() {
	}

	/**
	 * @param initialCapacity
	 */
	private EffectUnawareActuatorFireAbleCommandPrototypesCatalog(int initialCapacity) {
		super(initialCapacity);
	}

	/**
	 * @param c
	 */
	private EffectUnawareActuatorFireAbleCommandPrototypesCatalog(Collection<?> c) {
	}
	
	public static EffectUnawareActuatorFireAbleCommandPrototypesCatalog getInstance(){
		return EffectUnawareActuatorFireAbleCommandPrototypesCatalog.effectAgnosticActuatorFireAbleCommandPrototypesCatalog;
	}

	/**
	 * Fire a notification by its ID (key number)
	 * Let's call this "blind invocation"
	 * @param id is the number of the command to fire
	 * @return true if the command fire succeeded
	 * @throws DeviceAliasCommandExecutionException for problems encountered during command firing
	 */
	public boolean fireVirtualizedActuatorCommandbyId(int id) throws DeviceAliasCommandExecutionException {
		
		boolean result = false;
		
		if( (id < 0) || 
			(id >= this.size()) ){
			return result;
		}
		
		DeviceAliasPortCommand deviceAliasPortCommand = this.get(id);
		
		DeviceAliasPortController.getInstance().fireCommand(deviceAliasPortCommand);
		
		result = true;
		
		return result;
	}
	
	/**
	 * Prepare actually loading the commands
	 */
	private static void doPrepare(){
		
		DeviceAliasPortCommand deviceAliasPortCommandAccelerate = new DeviceAliasPortCommandImpl();    
		deviceAliasPortCommandAccelerate.setVerb(DeviceAliasPortCommandVerb.PORT_DRIVE_ACCELERATE);
		
		EffectUnawareActuatorFireAbleCommandPrototypesCatalog.getInstance().add(1, deviceAliasPortCommandAccelerate);
		
		DeviceAliasPortCommand deviceAliasPortCommandBrake = new DeviceAliasPortCommandImpl();    
		deviceAliasPortCommandBrake.setVerb(DeviceAliasPortCommandVerb.PORT_DRIVE_BRAKE);
		
		EffectUnawareActuatorFireAbleCommandPrototypesCatalog.getInstance().add(2, deviceAliasPortCommandBrake);
		
		DeviceAliasPortCommand deviceAliasPortCommandRight = new DeviceAliasPortCommandImpl();    
		deviceAliasPortCommandRight.setVerb(DeviceAliasPortCommandVerb.PORT_DRIVE_RIGHT);
		
		EffectUnawareActuatorFireAbleCommandPrototypesCatalog.getInstance().add(3, deviceAliasPortCommandRight);
		
		DeviceAliasPortCommand deviceAliasPortCommandLeft = new DeviceAliasPortCommandImpl();    
		deviceAliasPortCommandLeft.setVerb(DeviceAliasPortCommandVerb.PORT_DRIVE_LEFT);
		
		EffectUnawareActuatorFireAbleCommandPrototypesCatalog.getInstance().add(4, deviceAliasPortCommandLeft);
	}
	
}
